package com.mymusic.wangyiMusicBackend.controller;

import com.mymusic.wangyiMusicBackend.bean.Music;
import com.mymusic.wangyiMusicBackend.bean.PlayList;
import com.mymusic.wangyiMusicBackend.bean.ResultUtil;
import com.mymusic.wangyiMusicBackend.mapper.PlayListMapper;
import com.mymusic.wangyiMusicBackend.service.InfomationService;
import com.mymusic.wangyiMusicBackend.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

@RestController
@RequestMapping("/api/info")
public class InformationController {
    @Autowired
    InfomationService infomationService;

    @GetMapping ("/play-list")
    ResultUtil findAllPlayList(){
        return ResultUtil.success(infomationService.findAllPlayList());
    }

    @GetMapping("/play-list/{id}")
    ResultUtil findPlayListById(@PathVariable String id){
        try {
            int intId = Integer.parseInt(id);
            PlayList playList = infomationService.findPlayListById(intId);
            if(playList == null){
                return ResultUtil.fail(201,"not found music");
            }
            return ResultUtil.success(playList);
        }   catch (NumberFormatException e){
            return ResultUtil.fail(201,"Id must be number");
        }
    }

    @GetMapping("/insert/play-list/{musicId}")
    ResultUtil insertPlayListById(@PathVariable String musicId){
        if (musicId == null || musicId.trim() == ""){
            return ResultUtil.fail(201,"Id cannot be empty");
        }

        try {
            int intMusicId = Integer.parseInt(musicId);
            int affectRows = infomationService.insertPlayListByMusicId(intMusicId);
            if(affectRows == 0){
                return ResultUtil.fail(201,"add failed");
            }
            return ResultUtil.success(null);
        } catch (NumberFormatException e){
            return ResultUtil.fail(201,"Id must be number");
        }
    }

    @GetMapping("/delete/play-list/{id}")
    ResultUtil deletePlayListById(@PathVariable String id){
        if (id == null || id.trim() == ""){
            return ResultUtil.fail(201,"Id cannot be empty");
        }

        try {
            int intId = Integer.parseInt(id);
            int affectRows = infomationService.deletePlayListById(intId);
            if(affectRows == 0){
                return ResultUtil.fail(201,"delete failed");
            }
            return ResultUtil.success(null);
        } catch (NumberFormatException e){
            return ResultUtil.fail(201,"Id must be number");
        }
    }




    @GetMapping("/music/{id}")
    public ResultUtil findMusicById(@PathVariable String id){

        if (id == null || id.trim() == ""){
            return ResultUtil.fail(201,"Id cannot be empty");

        }

        try {
            int musicId = Integer.parseInt(id);
            Music music = infomationService.findMusicById(musicId);
            if(music == null){
                return ResultUtil.fail(201,"not found music");
            }
            return
                    ResultUtil.success(music);
        }   catch (NumberFormatException e){
            return ResultUtil.fail(201,"Id must be number");
        }


    }


    @GetMapping("/music")
    public ResultUtil findAllMusic(){

        return ResultUtil.success(infomationService.findAllMusic());

    }

    @GetMapping("/my-song-list")
    public ResultUtil findAllMySongList(){
        return ResultUtil.success(infomationService.findAllMySongList());
    }
}
